package com.buyantech.smweb.controller;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseSpringController;
import javacommon.util.DateTimeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.page.Page;

import com.buyantech.smweb.excel.ExcelAVieData;
import com.buyantech.smweb.model.Black;
import com.buyantech.smweb.model.CreditsExchange;
import com.buyantech.smweb.model.Ip;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.BlackManager;
import com.buyantech.smweb.service.CreditsExchangeManager;
import com.buyantech.smweb.service.IpManager;
import com.buyantech.smweb.service.UserManager;
import com.buyantech.smweb.vo.CreditsExchangeDto;
import com.buyantech.smweb.vo.query.BlackQuery;
import com.buyantech.smweb.vo.query.CreditsExchangeQuery;
import com.buyantech.smweb.vo.query.IpQuery;
import com.google.gson.Gson;

/**
 * 积分兑换记录
 * 
 * @version 1.0
 * @date 2012-05-10
 */
@Controller
public class IpController extends BaseSpringController {
	private IpManager ipManager;


	public void setIpManager(
			IpManager ipManager) {
		this.ipManager = ipManager;
	}

	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}

	/**
	 * 执行搜索
	 **/
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, IpQuery query) {

		Map<String, Object> modelMap = new HashMap<String, Object>();
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex")) + 1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile = request.getParameter("sortField");
		if (sortfile != null && !sortfile.equals(""))
			query.setSortColumns(request.getParameter("sortField") + " "
					+ request.getParameter("sortOrder"));
		else
			query.setSortColumns(" crdate desc");
		Page page = this.ipManager.searchPage(query);
		List<Ip> list = page.getResult();
		for (Ip ip : list) {
			if(ip.getCrdate()!=null)
				ip.setCrdate(ip.getCrdate().substring(2));
		}
		modelMap.put("data", list);
		modelMap.put("total", page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}

	/**
	 * 
	 **/
	public ModelAndView addIP(HttpServletRequest request,
			HttpServletResponse response)
			throws Exception {
		try {
			String json = request.getParameter("submitData");
			Gson gson = new Gson();
			Map<String, String> map = new HashMap<String, String>();
			map = gson.fromJson(json, Map.class);
			String remark = (String) map.get("remark");
			String ip= (String) map.get("ip");
			Ip mIp=new Ip();
			mIp.setIp(ip);
			mIp.setCrdate(DateTimeUtil.getDateTimeString());
			mIp.setRemark(remark);
			ipManager.save(mIp);
		} catch (Exception e) {
			ajaxPack("{state:false}", request, response);
			return null;
		}
		ajaxPack("{state:true}", request, response);

		return null;
	}
	
	/**
	 * 删除ip
	 **/
	public ModelAndView deleteIP(HttpServletRequest request,
			HttpServletResponse response)
			throws Exception {
		try {
			String ip = request.getParameter("ips");
			String[] ips = ip.split(",,,,,,,,,,");
			for (int i = 0; i < ips.length; i++) {
				 ipManager.remove("DELETE FROM  Ip where ip="+ips[i]);//切记hql
			}
		} catch (Exception e) {
			ajaxPack("{state:false}", request, response);
			return null;
		}
		ajaxPack("{state:true}", request, response);
		return null;
	}


}
